<template>
  <div class="pingjia-page">
    <!-- 头部 -->
    <header class="pingjia-header">
      <div class="header-content">
        <router-link to="/showshouye" class="go-back-btn">
          <el-icon><ArrowLeft /></el-icon>
        </router-link>
        <h2 class="title">店铺评论区</h2>
        <el-button
            type="primary"
            @click="doPingJiaAnalysis"
            class="analysis-btn"
        >
          <el-icon><DataAnalysis /></el-icon>
          评论分析
        </el-button>
      </div>
    </header>

    <el-main class="main-container">
      <el-card shadow="never" class="pingjia-container">
        <el-empty
            v-if="myPingjia.length === 0"
            :image="emptyPingjiaImg"
            description="暂无可回复评价"
            :image-size="100"
        />

        <el-timeline v-else>
          <el-timeline-item
              v-for="(item, index) in myPingjia"
              :key="index"
              :timestamp="item.pjsj"
              placement="top"
              class="timeline-item"
          >
            <el-card shadow="hover" class="pingjia-card">
              <!-- 用户信息区 -->
              <div class="user-info">
                <el-avatar :src="url+item.yhtx" class="user-avatar" />
                <div class="user-detail">
                  <span class="user-name">{{ item.yhnc }}</span>
                  <el-rate
                      v-model="item.sjpf"
                      disabled
                      show-score
                      text-color="#ff9900"
                      score-template="{value}分"
                      class="user-rate"
                  />
                </div>
              </div>

              <!-- 菜品信息 -->
              <div v-if="item.cpmc" class="dish-info">
                <el-tag type="info" class="dish-tag">{{ item.cpmc }}</el-tag>
                <el-tag v-if="item.cpbq" type="success" class="dish-tag">{{ item.cpbq }}</el-tag>
              </div>

              <!-- 评价内容 -->
              <div class="pingjia-content">
                <p class="pingjia-text">{{ item.pjnr }}</p>

                <!-- 图片展示使用el-image组件 -->
                <div v-if="item.sctp" class="image-gallery">
                  <el-image
                      v-for="(img, imgIndex) in item.sctp.split(',')"
                      :key="imgIndex"
                      :src="url + img"
                      :preview-src-list="item.sctp.split(',').map(i => url + i)"
                      fit="cover"
                      class="image-item"
                      hide-on-click-modal
                  />
                </div>
              </div>

              <!-- 商家回复区 -->
              <div v-if="item.sjhf" class="reply-section">
                <div class="reply-header">
                  <el-icon class="reply-icon"><ChatLineSquare /></el-icon>
                  <span class="reply-title">商家回复</span>
                </div>
                <div class="reply-content">
                  <p>{{ item.sjhf }}</p>
                  <span class="reply-time">{{ item.hfsj }}</span>
                </div>
              </div>

              <!-- 回复按钮 -->
              <div v-else class="action-buttons">
                <el-button
                    type="primary"
                    size="large"
                    @click="openReplyDialog(item)"
                    class="reply-btn"
                >
                  <el-icon><Edit /></el-icon>
                  回复评价
                </el-button>
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </el-card>
    </el-main>

    <el-dialog v-model="SJHFVisible" title="回复评价" width="50%" class="reply-dialog">
      <el-form :model="replyForm" ref="replyFormRef" label-width="100px">
        <el-form-item label="回复内容" prop="sjhf">
          <el-input
              v-model="replyForm.sjhf"
              type="textarea"
              :autosize="{ minRows: 5, maxRows: 10 }"
              placeholder="请输入您的回复内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="SJHFVisible = false">取消</el-button>
        <el-button type="primary" @click="SJHF">提交回复</el-button>
      </template>
    </el-dialog>

    <!--评价数据分析 弹框-->
    <el-dialog
        v-model="pingJiaAnalysisDialogVisible"
        title="用户评论分析"
        fullscreen
        class="analysis-dialog"
    >
      <s-j-p-j-analysis
          @close="pingJiaAnalysisDialogVisible = false">
      </s-j-p-j-analysis>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import emptyPingjiaImg from '../../assets/logo1.png';
import axios from "axios";
import {ElMessage} from "element-plus";
import { ArrowLeft, DataAnalysis, ChatLineSquare, Edit } from '@element-plus/icons-vue';
import SJPJAnalysis from "./sjpjAnalysis.vue";

const url = "http://localhost:8088/team/upload/"
// 路由实例
const router = useRouter();
const myPingjia = ref([]);
const getSJpj = async () => {
  const {data} = await axios.get(`/api/pingjia/sjplq/${userInfo.value.sjid}`)
  console.log(data)
  if(data.code === 200){
    myPingjia.value = data.data;
  }
}

const userInfo = ref({});
const SJHFVisible = ref(false);
const openReplyDialog = async (item) => {
  SJHFVisible.value = true;
  replyForm.value.pjid = item.pjid;
  replyForm.value.sjhf = item.sjhf;
}
const SJHF = async () => {
  const shanghaiDate = new Date(Date.now() + 8 * 60 * 60 * 1000);
  replyForm.value.hfsj = shanghaiDate.toISOString().slice(0, 19).replace('T', ' ');
  const {data} = await axios.put(`/api/pingjia`,replyForm.value)
  console.log(data)
  if(data.code === 200){
    ElMessage.success('回复成功')
    SJHFVisible.value = false;
    await getSJpj()
  }
}
const replyForm = ref({
  sjhf: ''
})

//评价分析
const pingJiaAnalysisDialogVisible = ref(false);
function doPingJiaAnalysis() {
  pingJiaAnalysisDialogVisible.value = true;
}

// 页面挂载时加载数据
onMounted(() => {
  userInfo.value = JSON.parse(localStorage.getItem('userInfo')) || {};
  getSJpj()
});
</script>

<style scoped>
.pingjia-page {
  font-family: "Microsoft YaHei", sans-serif;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  min-height: 100vh;
  padding-bottom: 30px;
}

.pingjia-header {
  background: linear-gradient(135deg, #f58147 0%, #ff6b35 100%);
  color: white;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
}

.title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
}

.go-back-btn {
  color: white;
  text-decoration: none;
  font-size: 1.5rem;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.go-back-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.analysis-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  padding: 10px 20px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.analysis-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
}

/* 主要内容区样式 */
.main-container {
  padding: 25px;
  max-width: 1200px;
  margin: 0 auto;
}

.pingjia-container {
  border-radius: 16px;
  background: transparent;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  overflow: hidden;
  backdrop-filter: blur(10px);
}

/* 时间线样式 */
:deep(.el-timeline) {
  padding: 20px;
}

.timeline-item {
  padding-bottom: 30px;
}

:deep(.el-timeline-item__tail) {
  border-left: 2px solid #f58147;
}

:deep(.el-timeline-item__node) {
  background: #f58147;
  width: 16px;
  height: 16px;
  border: 3px solid white;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

/* 评价卡片样式 */
.pingjia-card {
  margin-bottom: 30px;
  border-radius: 16px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
  border: none;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background: white;
  overflow: hidden;
}

.pingjia-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 20px;
  padding: 20px 20px 0 20px;
}

.user-avatar {
  width: 60px !important;
  height: 60px !important;
  border: 3px solid #f58147;
  box-shadow: 0 4px 12px rgba(245, 129, 71, 0.3);
}

.user-detail {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.user-name {
  font-weight: 700;
  font-size: 1.2rem;
  color: #333;
}

.user-rate :deep(.el-rate__icon) {
  font-size: 1.2rem;
}

.dish-info {
  margin: 0 20px 20px 20px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.dish-tag {
  border-radius: 20px;
  padding: 6px 15px;
  font-size: 0.9rem;
}

.pingjia-content {
  margin: 0 20px 20px 20px;
}

.pingjia-text {
  margin: 0 0 20px;
  line-height: 1.7;
  font-size: 1.1rem;
  color: #555;
}

.image-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 15px;
}

.image-item {
  width: 100px;
  height: 100px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.image-item:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

/* 商家回复区样式 */
.reply-section {
  margin: 0 20px 20px 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
  background: #f9f9f9;
  border-radius: 12px;
  padding: 20px;
}

.reply-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 15px;
}

.reply-icon {
  color: #f58147;
  font-size: 1.3rem;
}

.reply-title {
  color: #f58147;
  font-size: 1.2rem;
  font-weight: 600;
}

.reply-content {
  background: white;
  padding: 15px;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.reply-content p {
  margin: 0 0 10px;
  font-size: 1rem;
  color: #666;
  line-height: 1.6;
}

.reply-time {
  font-size: 0.9rem;
  color: #999;
  display: block;
  text-align: right;
}

.action-buttons {
  margin: 0 20px 20px 20px;
  text-align: right;
}

.reply-btn {
  background: linear-gradient(135deg, #f58147 0%, #ff6b35 100%);
  border: none;
  padding: 12px 25px;
  border-radius: 8px;
  font-weight: 500;
  font-size: 1rem;
  box-shadow: 0 4px 15px rgba(245, 129, 71, 0.3);
  transition: all 0.3s ease;
}

.reply-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(245, 129, 71, 0.4);
}

/* 对话框样式 */
.reply-dialog :deep(.el-dialog) {
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
}

.reply-dialog :deep(.el-dialog__header) {
  background: linear-gradient(135deg, #f58147 0%, #ff6b35 100%);
  color: white;
  padding: 20px;
}

.reply-dialog :deep(.el-dialog__title) {
  color: white;
  font-weight: 600;
}

.reply-dialog :deep(.el-dialog__headerbtn .el-dialog__close) {
  color: white;
}

.analysis-dialog :deep(.el-dialog) {
  border-radius: 0;
}

.analysis-dialog :deep(.el-dialog__header) {
  background: linear-gradient(135deg, #f58147 0%, #ff6b35 100%);
  color: white;
  padding: 20px;
}

.analysis-dialog :deep(.el-dialog__title) {
  color: white;
  font-weight: 600;
}

.analysis-dialog :deep(.el-dialog__headerbtn .el-dialog__close) {
  color: white;
  font-size: 1.5rem;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .pingjia-header {
    padding: 15px;
  }

  .header-content {
    flex-direction: column;
    gap: 15px;
  }

  .main-container {
    padding: 15px;
  }

  .pingjia-card {
    margin-bottom: 20px;
  }

  .user-info {
    padding: 15px 15px 0 15px;
  }

  .user-avatar {
    width: 50px !important;
    height: 50px !important;
  }

  .user-name {
    font-size: 1.1rem;
  }

  .dish-info, .pingjia-content, .reply-section, .action-buttons {
    margin: 0 15px 15px 15px;
  }

  .image-item {
    width: 80px;
    height: 80px;
  }

  .reply-btn {
    width: 100%;
  }

  :deep(.el-timeline) {
    padding: 10px;
  }
}
</style>
